package com.mlnx.web.device.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mlnx.common.entity.Response;
import com.mlnx.common.entity.ResponseData;
import com.mlnx.common.form.PageForm;
import com.mlnx.device.entity.Device;
import com.mlnx.device.pojo.dto.BpDeviceDto;
import com.mlnx.device.pojo.form.BpDeviceFilterForm;
import com.mlnx.device.pojo.form.BpDeviceForm;
import com.mlnx.device.service.BpDeviceService;
import com.mlnx.device.service.DeviceService;
import com.mlnx.service.common.enums.ResponseEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.shan.spring.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


@Api(value = "血压设备接口", tags = {"血压设备接口"})
@RestController
@RequestMapping("device/bp")
public class BpDeviceController extends BaseController {

    @Autowired
    private BpDeviceService bpDeviceService;

    @Autowired
    private DeviceService deviceService;

    @ApiOperation(value = "添加血压设备")
    @PostMapping(value = "")
    public Response addBpDevice(@Valid BpDeviceForm bpDeviceForm, HttpServletRequest httpServletRequest) {
        Device device = deviceService.getOne(new QueryWrapper<Device>().eq("numeration", bpDeviceForm.getNumeration()));
        if (device != null)
            if (device.getStatus() == 1)
                deviceService.removeById(device.getId());
            else
                return result(ResponseEnum.DeviceExisted);
        Integer userId = Integer.parseInt((String) httpServletRequest.getAttribute("username"));
        bpDeviceService.addBpDevice(bpDeviceForm,userId);

        return result();
    }

    @ApiOperation(value = "分页获取血压设备", notes = "")
    @GetMapping(value = "")
    public ResponseData<Page<BpDeviceDto>> list(@Valid PageForm pageForm, BpDeviceFilterForm bpDeviceFilterForm,
                                                HttpServletRequest httpServletRequest) {
        Integer userId = Integer.parseInt((String) httpServletRequest.getAttribute("username"));
        if(userId != 1){
            bpDeviceFilterForm.setUserId(userId);
        }
        return result(bpDeviceService.list(pageForm, bpDeviceFilterForm));
    }

    @ApiOperation(value = "获取在线血压设备", notes = "")
    @GetMapping(value = "/online")
    public ResponseData<List<BpDeviceDto>> listOnlines(BpDeviceFilterForm bpDeviceFilterForm) {

        return result(bpDeviceService.listOnlines(bpDeviceFilterForm));
    }

    @ApiOperation(value = "批量删除血压设备", notes = "")
    @DeleteMapping(value = "")
    public Response deleteBpDevices(Integer[] ids) {

        deviceService.deleteDevices(ids);

        return result();
    }

    @ApiOperation(value = "修改血压设备信息", notes = "")
    @PutMapping(value = "/{id}")
    public Response editBpDevice(@PathVariable("id") Integer id, @Valid BpDeviceForm bpDeviceForm) {

        bpDeviceService.updateBpDevice(id, bpDeviceForm);

        return result();
    }
}
